<?php
App::uses('AppController', 'Controller');

class JobsController extends AppController {

	public $uses = array('Job');
        function beforeFilter() {
            parent::beforeFilter();
            $AppInformation['Controller']['nameSingular'] = "notícia";
            $AppInformation['Controller']['namePlural'] = "notícias";
            $this->set("AppInformation",$AppInformation);
        }

				function index()
				{
						$this->layout = "default";

						$Jobs = $this->Job->find("all");
						$this->set(compact("Jobs"));
				}

				function view()
				{
						$Jobs = $this->Job->find("all");

						$this->set('Jobs',$Jobs);
				}

        function admin_index()
        {
            $this->layout = "admin";
            $Jobs = $this->Job->find("all");

            $this->set('Jobs',$Jobs);
        }
        function admin_add()
        {
            $this->layout = "admin";

            if ($this->request->is('post'))
            {
							//verifica se houve envio de imagem - avatar
							if(empty($this->request->data['Image'][0]['attachment']['name']))
								unset($this->request->data['Image'][0]);

         				//die(debug($this->request->data));
                $this->Job->createWithAttachments($this->request->data);
                $this->Session->setFlash("Salvo com sucesso!");

                // Trocar 2 por getInsertLastInsert
                $this->redirect(array('action'=>'index'));
            }
        }
        function admin_edit($id=null)
        {
            $this->layout = "admin";

            if ($this->request->is(array('post', 'put')))
            {

							  //verifica se houve envio de imagem - avatar
					      if(empty($this->request->data['Image'][0]['attachment']['name']))
					        unset($this->request->data['Image'][0]);
                //die(debug($this->request->data));
                $this->Job->createWithAttachments($this->request->data);
								$this->Session->setFlash(__('Salvo com sucesso!'), 'Flashs/FlashMessengeGood');
								$this->redirect(array('action'=>'edit',$id));

            }
            else
            {
                $this->data = $this->Job->find("first", array("recursive"=>2,"conditions"=>array("id"=>$id)));
            }
        }

        function admin_delete($id)
        {
            $this->Job->delete($id);
            $this->Session->setFlash('Removido com sucesso.', 'default', array(), 'good');
            $this->redirect('/admin/Jobs');
        }
}
